<?php

namespace App\Imports;

use App\Models\Book;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithHeadingRow;

class BooksImport implements ToModel,WithHeadingRow
{
    /**
    * @param array $row
    *
    * @return \Illuminate\Database\Eloquent\Model|null
    */
    public function model(array $row)
    {
        //print_r($row);
        if(!empty($row['title']))
        {
            $book = new Book([
                'title' => isset($row['title'])?$row['title']:'',
                'author' => isset($row['author'])?$row['author']:'',
                'intro' => isset($row['intro'])?$row['intro']:'',
                'category_id' => isset($row['category_id'])?$row['category_id']:0,
                'category_parent_id' => isset($row['category_parent_id'])?$row['category_parent_id']:0,
                'catalog' => 0,
            ]);
            $book->save();

            \DB::table('book_publish_infos')->insert([
                'book_id' => $book->id,
                'writer' => isset($row['writer'])?$row['writer']:'',
                'publish_house' => isset($row['publish_house'])?$row['publish_house']:'',
                'isbn' => isset($row['isbn'])?$row['isbn']:'',
                'publish_num' => isset($row['publish_num'])?(int)$row['publish_num']:0,
                'package' => isset($row['package'])?$row['package']:'',
                'book_size' => isset($row['book_size'])?$row['book_size']:'',
                'publish_time' => isset($row['publish_time'])?$row['publish_time']:'',
                'use_paper' => isset($row['use_paper'])?$row['use_paper']:'',
                'page_sizes' => isset($row['page_sizes'])?$row['page_sizes']:'',
                'language' => isset($row['language'])?$row['language']:'',
            ]);
        }

        /*return new Book([
            //
        ]);*/
    }
}
